const fs = require("node:fs");
const path = require("node:path");

const companyMapper = require("../mapper/companyMapper.js");
const projectMapper = require("../mapper/projectMapper.js");
const itemMapper = require("../mapper/itemMapper.js");
const {db} = require("../db/db");

function add(company) {
    return new Promise((resolve, reject) => { 
        if (typeof company.name != "string" || company.name == "") {
            reject("parameter error")
            return;
        }
        
        companyMapper.selectCompanyIdByName(company.name, (err, companyIds) => {
            if (err) { reject(err); return; }
        
            if (companyIds.length > 0) {
                reject(company.name +" exists")
                return;
            } 

            companyMapper.addCompany(company, function(err){
                if (err) { reject(err); return; }
                resolve(this.lastID)
            })
        })
    })  
}
exports.add = add

function get(companyId) {
    return new Promise((resolve, reject) => {
        companyMapper.selectCompanyById(companyId, (err, company) => {
            if (err != null) { reject(err); return; }
            resolve(company);
        })
    })
}
exports.get = get

function getAll() {
    return new Promise((resolve, reject) => {
        companyMapper.selectAllCompany((err, companies) => {
            if (err != null) { reject(err); return; }
            resolve(companies);
        })
    })
}
exports.getAll = getAll


function deleteEntity(companyId) {
    return new Promise((resolve, reject) => {
        if (!companyId) {
            reject("parameter error")
            return;
        }
        
        db.serialize(() => {
            db.run("begin");
    
            itemMapper.deleteItemsByCompanyId(companyId, (err) => {
                if (err) { reject(err); return; }
            })
    
            projectMapper.deleteProjectsByCompanyId(companyId, (err) => {
                if (err) { reject(err); return; }
            })
            
            companyMapper.deleteCompanyById(companyId, (err) => {
                if (err) { reject(err); return; }
                resolve()
            })
    
            db.run("commit");
        })
    })
}
exports.delete = deleteEntity

function update(entity) {
    return new Promise((resolve, reject) => { 
        if (!entity.id || !entity.name) {
            reject("parameter error")
            return;
        }
        
        companyMapper.selectCompanyIdByName(entity.name, (err, companyIds) => {
            if (err) { reject(err); return; }
        
            if (companyIds.length > 1 || (companyIds.length == 1 && companyIds[0].id != entity.id)) {
                reject(entity.name +" exists")
                return;
            } 

            companyMapper.updateCompany(entity, (err) => {
                if (err) { reject(err); return; }
                resolve()
            })
        })
    })  
}
exports.update = update

function saveAllCompanies() { 
    return new Promise((resolve, reject) => {
        companyMapper.selectAllCompany((err, rows) => {
            if (err) {
                reject(err);
                return
            }
       
            var writer = fs.createWriteStream(path.join(__dirname, "../../core/static/js2/company.js"));
            writer.write("var _companys = ");
            writer.write(JSON.stringify(rows));
            writer.end(';');
    
            resolve()
        })
    })
}

exports.saveAllCompanies = saveAllCompanies

function saveDataToLocal() {
    saveAllCompanies()
}